package com.myzl.coal.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myzl.coal.domain.AlarmEvent;
import com.myzl.coal.domain.Rfid;
import com.myzl.coal.mapper.AlarmEventMapper;
import com.myzl.coal.mq.MqService;
import com.myzl.coal.service.IAlarmEventService;
import com.myzl.coal.service.IRfidService;
import com.myzl.common.enums.AlarmEventType;
import com.myzl.common.enums.DeviceStatusEnum;
import com.myzl.common.enums.HandleStatusEnum;
import com.myzl.common.enums.PlatDictValueEnum;
import com.myzl.common.exception.CustomException;
import com.myzl.common.utils.DateUtils;
import com.myzl.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 事件Service业务层处理
 *
 * @author myzl
 * @date 2023-01-06
 */
@Service
public class AlarmEventServiceImpl extends ServiceImpl<AlarmEventMapper, AlarmEvent> implements IAlarmEventService
{
    @Autowired
    private AlarmEventMapper alarmEventMapper;
    @Autowired
    private IRfidService rfidService;
    @Autowired
    private MqService mqService;

    @Override
    public void rfidPolice(List<Rfid> offlineList) {
        offlineList.forEach(e -> e.setStatus(DeviceStatusEnum.OFFLINE.getCode()));
        rfidService.updateBatchById(offlineList);

        Map<Integer, List<Rfid>> map = offlineList.stream().collect(Collectors.groupingBy(Rfid::getDeviceId, Collectors.toList()));
        List<AlarmEvent> alarmEventAddList = new ArrayList<>();
        map.forEach((k, v) -> {
            v.forEach(e -> {
                String content = "第"+e.getSerialNumber()+"块筛板脱落";
                AlarmEvent alarmEvent = new AlarmEvent();
                alarmEvent.setDictValue(PlatDictValueEnum.FALL_OFF.getCode()+e.getSerialNumber());
                alarmEvent.setDeviceId(k);
                alarmEvent.setTitle("筛板脱落");
                alarmEvent.setEventType(AlarmEventType.SIEVE.getCode());
                alarmEvent.setContent(content);
                alarmEventAddList.add(alarmEvent);
            });

        });

        if (!alarmEventAddList.isEmpty()) {
            mqService.sendPolice(alarmEventAddList);
        }
    }

    @Override
    public void rfidRestore(List<Rfid> onlineList) {
        onlineList.forEach(e -> e.setStatus(DeviceStatusEnum.ONLINE.getCode()));
        rfidService.updateBatchById(onlineList);

        Map<Integer, List<Rfid>> map = onlineList.stream().collect(Collectors.groupingBy(Rfid::getDeviceId, Collectors.toList()));
        List<AlarmEvent> alarmEventAddList = new ArrayList<>();
        map.forEach((k, v) -> {
            v.forEach(e -> {
                AlarmEvent alarmEvent = new AlarmEvent();
                alarmEvent.setDictValue(PlatDictValueEnum.FALL_OFF.getCode()+e.getSerialNumber());
                alarmEvent.setDeviceId(k);
                alarmEvent.setHandleStatus(HandleStatusEnum.UNHANDLED.getCode());
                List<AlarmEvent> alarmEvents = alarmEventMapper.selectAlarmEventList(alarmEvent);
                if (CollectionUtils.isEmpty(alarmEvents)){
                    return;
                }
                alarmEventAddList.addAll(alarmEvents);
            });

        });

        if (!alarmEventAddList.isEmpty()) {
            mqService.sendPolice(alarmEventAddList);
        }

    }


    /**
     * 查询事件
     *
     * @param id 事件主键
     * @return 事件
     */
    @Override
    public AlarmEvent selectAlarmEventById(Long id)
    {
        return alarmEventMapper.selectAlarmEventById(id);
    }


    /**
     * 查询事件列表
     *
     * @param alarmEvent 事件
     * @return 事件
     */
    @Override
    public List<AlarmEvent> selectAlarmEventList(AlarmEvent alarmEvent)
    {
        return alarmEventMapper.selectAlarmEventList(alarmEvent);
    }

    /**
     * 新增事件
     *
     * @param alarmEvent 事件
     * @return 结果
     */
    @Override
    public int insertAlarmEvent(AlarmEvent alarmEvent)
    {
        return alarmEventMapper.insertAlarmEvent(alarmEvent);
    }

    /**
     * 修改事件
     *
     * @param alarmEvent 事件
     * @return 结果
     */
    @Override
    public int updateAlarmEvent(AlarmEvent alarmEvent)
    {
        return alarmEventMapper.updateAlarmEvent(alarmEvent);
    }

    /**
     * 批量删除事件
     *
     * @param ids 需要删除的事件主键
     * @return 结果
     */
    @Override
    public int deleteAlarmEventByIds(Long[] ids)
    {
        return alarmEventMapper.deleteAlarmEventByIds(ids);
    }

    /**
     * 删除事件信息
     *
     * @param id 事件主键
     * @return 结果
     */
    @Override
    public int deleteAlarmEventById(Long id)
    {
        return alarmEventMapper.deleteAlarmEventById(id);
    }

    @Override
    public int handleOne(AlarmEvent alarmEvent) {
        AlarmEvent selectAlarmEvent = alarmEventMapper.selectAlarmEventById(alarmEvent.getId());
        if (Objects.equals(HandleStatusEnum.PROCESSED.getCode(),selectAlarmEvent.getHandleStatus())){
            throw new CustomException("该事件已处理");
        }
        alarmEvent.setHandleStatus(HandleStatusEnum.PROCESSED.getCode());
        alarmEvent.setHandler(SecurityUtils.getLoginUser().getUser().getNickName());
        alarmEvent.setHandleTime(DateUtils.getNowDate());
        return alarmEventMapper.updateAlarmEvent(alarmEvent);
    }

    @Override
    public boolean handleMore(Long[] ids) {
        boolean result = true;
        List<AlarmEvent> alarmEventList = new ArrayList<>();
        for (Long id : ids) {
            AlarmEvent selectAlarmEvent = alarmEventMapper.selectAlarmEventById(id);
            if (Objects.equals(HandleStatusEnum.PROCESSED.getCode(),selectAlarmEvent.getHandleStatus())){
                continue;
            }
            AlarmEvent alarmEvent = new AlarmEvent();
            alarmEvent.setId(id);
            alarmEvent.setHandleStatus(HandleStatusEnum.PROCESSED.getCode());
            alarmEvent.setHandler(SecurityUtils.getLoginUser().getUser().getNickName());
            alarmEvent.setHandleTime(DateUtils.getNowDate());
            alarmEvent.setHandleResult("批量处理");
            alarmEventList.add(alarmEvent);
        }
        if (alarmEventList.size() > 0){
            result = this.updateBatchById(alarmEventList);
        }
        return result;
    }
}
